using UnityEngine;

public class BulletBall : MonoBehaviour
{
	public float speed;

	public Vector3 startPosition;

	public Vector3 endPosition;

	private Vector3 startScale;

	public bool startClip;

	public Vector4 startClipPlaneCoef;

	public bool endClip;

	public Vector4 endClipPlaneCoef;

	private AudioSource soundSource;

	public AudioClip soundCollision;

	private bool isAlive;

	private Vector3 rotate = Vector3.zero;

	private Vector3 direction = Vector3.zero;

	private float distanceTotal;

	private float distanceCurrent;

	private bool finalShrink = true;

	private void Awake()
	{
		if (soundCollision != null)
		{
			soundSource = base.gameObject.AddComponent<AudioSource>();
			soundSource.clip = soundCollision;
			soundSource.loop = false;
			soundSource.rolloffMode = AudioRolloffMode.Linear;
			soundSource.maxDistance = 30f;
			soundSource.dopplerLevel = 0.1f;
		}
		EventManager.WorldResetEvent += OnWorldReset;
	}

	private void OnDestroy()
	{
		EventManager.WorldResetEvent -= OnWorldReset;
	}

	private void OnWorldReset()
	{
		isAlive = false;
		base.GetComponent<Renderer>().enabled = false;
		Object.Destroy(base.gameObject);
	}

	public void Shot(Vector3 start_, Vector3 end_, float speed_, bool shrink = true)
	{
		startScale = base.transform.localScale;
		speed = speed_;
		startPosition = start_;
		endPosition = end_;
		finalShrink = shrink;
		direction = (endPosition - startPosition).normalized;
		distanceTotal = Vector3.Distance(startPosition, endPosition);
		distanceCurrent = 0f;
		base.GetComponent<Rigidbody>().isKinematic = false;
		rotate = new Vector3(Random.Range(0f, 2f) - 1f, Random.Range(0f, 2f) - 1f, Random.Range(0f, 2f) - 1f).normalized;
		rotate = base.transform.TransformPoint(Vector3.up);
		base.GetComponent<Rigidbody>().angularVelocity = rotate * Random.Range(3f, 6f);
		base.GetComponent<Rigidbody>().transform.position = startPosition;
		base.GetComponent<Renderer>().enabled = true;
		isAlive = true;
	}

	private void OnCollisionEnter()
	{
		if (soundCollision != null)
		{
			float volume = Mathf.Clamp(speed / 15f, 0f, 1f);
			Audio.PlayShotSound(soundCollision, volume, soundSource);
		}
	}

	private void FixedUpdate()
	{
		if (isAlive)
		{
			base.GetComponent<Rigidbody>().velocity = direction * speed;
			if (Mathf.Abs(base.GetComponent<Rigidbody>().transform.localPosition.x) > 0.01f || Mathf.Abs(base.GetComponent<Rigidbody>().transform.localPosition.y) > 0.01f)
			{
				base.GetComponent<Rigidbody>().MovePosition(base.transform.parent.TransformPoint(Vector3.forward * base.GetComponent<Rigidbody>().transform.localPosition.z));
			}
			distanceCurrent = Vector3.Distance(base.GetComponent<Rigidbody>().transform.position, startPosition);
			if (finalShrink && distanceCurrent > distanceTotal * 0.75f)
			{
				base.GetComponent<Rigidbody>().transform.localScale = startScale * ((distanceTotal - distanceCurrent) / (distanceTotal * 0.25f));
			}
			if (distanceCurrent >= distanceTotal)
			{
				base.GetComponent<Renderer>().enabled = false;
				Object.Destroy(base.gameObject);
				Game.UnloadResourcesLater();
			}
		}
	}
}
